using System;

namespace Leo.Core.Abp.Client.Contracts
{
    /// <summary>
    /// 响应数据静态输出
    /// </summary>
    [Serializable]
    public class ApiResult
    {
        /// <summary>
        /// 成功
        /// </summary>
        /// <returns></returns>
        public static ApiResult<bool> Succees()
        {
            return new ApiResult<bool>().Succees(true);
        }

        /// <summary>
        /// 成功
        /// </summary>
        /// <returns></returns>
        public static ApiResult<T> Succees<T>()
        {
            return new ApiResult<T>().Succees();
        }

        /// <summary>
        /// 成功
        /// </summary>
        /// <param name="msg">消息</param>
        /// <returns></returns>
        public static ApiResult<string> Succees(string msg)
        {
            return new ApiResult<string>().Succees(msg);
        }

        /// <summary>
        /// 成功
        /// </summary>
        /// <param name="data">数据</param>
        /// <returns></returns>
        public static ApiResult<T> Succees<T>(T data)
        {
            return new ApiResult<T>().Succees(data);
        }

        /// <summary>
        /// 成功
        /// </summary>
        /// <param name="data">数据</param>
        /// <param name="msg">消息</param>
        /// <returns></returns>
        public static ApiResult<T> Succees<T>(string msg, T data)
        {
            return new ApiResult<T>().Succees(msg, data);
        }

        /// <summary>
        /// 失败
        /// </summary>
        /// <returns></returns>
        public static ApiResult<T> Error<T>()
        {
            return Error<T>();
        }

        /// <summary>
        /// 失败
        /// </summary>
        /// <param name="msg">消息</param>
        /// <returns></returns>
        public static ApiResult<T> Error<T>(string msg)
        {
            return new ApiResult<T>().Error(msg);
        }

        /// <summary>
        /// 失败
        /// </summary>
        /// <param name="data">数据</param>
        /// <returns></returns>
        public static ApiResult<T> Error<T>(T data)
        {
            return new ApiResult<T>().Error(data);
        }

        /// <summary>
        /// 失败
        /// </summary>
        /// <param name="msg">消息</param>
        /// <param name="data">数据</param>
        /// <returns></returns>
        public static ApiResult<T> Error<T>(string msg, T data)
        {
            return new ApiResult<T>().Error(msg, data);
        }

        /// <summary>
        /// 根据布尔值返回结果
        /// </summary>
        /// <param name="success"></param>
        /// <returns></returns>
        public static ApiResult<T> Result<T>(bool success)
        {
            return success ? Succees<T>() : Error<T>();
        }
    }

    /// <summary>
    /// 响应数据输出
    /// </summary>
    [Serializable]
    public class ApiResult<T> : ApiResult
    {
        /// <summary>
        /// 是否成功标记
        /// </summary>
        public bool Success { get; private set; }

        /// <summary>
        /// 状态码
        /// </summary>
        public int Code => Success ? 1 : 0;

        /// <summary>
        /// 消息
        /// </summary>
        public string Message { get; private set; }

        /// <summary>
        /// 数据
        /// </summary>
        public T Data { get; private set; }

        /// <summary>
        /// 成功
        /// </summary>
        /// <param name="data">数据</param>
        public ApiResult<T> Succees()
        {
            Success = true;
            Message = "操作成功";
            Data = default;
            return this;
        }

        /// <summary>
        /// 成功
        /// </summary>
        /// <param name="data">数据</param>
        public ApiResult<T> Succees(T data)
        {
            Success = true;
            Data = data;
            Message = "操作成功";
            return this;
        }

        /// <summary>
        /// 成功
        /// </summary>
        /// <param name="msg">消息</param>
        public ApiResult<T> Succees(string msg)
        {
            Success = true;
            Message = msg;
            return this;
        }

        /// <summary>
        /// 成功
        /// </summary>
        /// <param name="data">数据</param>
        /// <param name="msg">消息</param>
        public ApiResult<T> Succees(string msg, T data)
        {
            Success = true;
            Data = data;
            Message = msg;
            return this;
        }


        /// <summary>
        /// 失败
        /// </summary>
        /// <param name="data">数据</param>
        /// <returns></returns>
        public ApiResult<T> Error(T data)
        {
            Success = false;
            Message = "操作失败";
            Data = data;
            return this;
        }

        /// <summary>
        /// 失败
        /// </summary>
        /// <param name="msg">消息</param>
        /// <returns></returns>
        public ApiResult<T> Error(string msg)
        {
            Success = false;
            Message = msg;
            Data = default;
            return this;
        }

        /// <summary>
        /// 失败
        /// </summary>
        /// <param name="msg">消息</param>
        /// <param name="data">数据</param>
        /// <returns></returns>
        public ApiResult<T> Error(string msg, T data)
        {
            Success = false;
            Message = msg;
            Data = data;
            return this;
        }
    }
}